package com.speljohan.rsbot.bot;

import static com.speljohan.rsbot.util.GlobalConfiguration.OperatingSystem.MAC;
import static com.speljohan.rsbot.util.GlobalConfiguration.OperatingSystem.WINDOWS;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.speljohan.rsbot.util.GlobalConfiguration;

/**
 * @author illusion (modifications by ins3)
 */
public final class ParameterParser {
	private static final Logger log = Logger.getLogger(ParameterParser.class.getName());

	private static final String PARAMETER_PATTERN = "<param name=\"?([^\\s]+)\"?\\s+value=\"?([^>]*)\"?>";

	public static HashMap<String, String> getParameters(final String world) throws Exception {

		final HashMap<String, String> parameters = new HashMap<String,String>();

		final String type = Integer.parseInt(world) % 2 == 0 ? "1" : "0";
		final String url = "http://world" + world + ".runescape.com/m" + type + ",j0";
		ParameterParser.log.fine("Getting parameters from " + url);
		final URL runPage = new URL(url);
		ParameterParser.log.fine(runPage.toString());
		final String html = ParameterParser.read(runPage);
		ParameterParser.log.finest(html);
		final Pattern regex = Pattern.compile(ParameterParser.PARAMETER_PATTERN, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		final Matcher regexMatcher = regex.matcher(html);

		while(regexMatcher.find()){

			final String key = regexMatcher.group(1);
			final String value = regexMatcher.group(2);

			if(!parameters.containsKey(key)) {
				parameters.put(key, value);
			}
		}

		parameters.remove("haveie6");
		ParameterParser.log.finest("Parameters parsed were: " + parameters);
		return parameters;
	}

	private static String read(final URL url) throws IOException, InterruptedException {
		String referer = "runescape.com";
		final String returnString = ParameterParser.read(url, referer);
		referer = url.toExternalForm();
		return returnString;
	}

	private static String read(final URL url, final String referer) throws IOException, InterruptedException, NegativeArraySizeException {
		final URLConnection uc = url.openConnection();
		uc.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		uc.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		uc.addRequestProperty("Accept-Encoding", "gzip,deflate");
		uc.addRequestProperty("Accept-Language", "en-us,en;q=0.5");
		uc.addRequestProperty("Connection", "keep-alive");
		uc.addRequestProperty("Host", url.getHost());
		uc.addRequestProperty("Keep-Alive", "300");
		if (referer != null) {
			uc.addRequestProperty("Referer", referer);
		}

		String agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)";
		if (GlobalConfiguration.isOperatingSystem(MAC)) {
			agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/530.7+ (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";
		} else if (!GlobalConfiguration.isOperatingSystem(WINDOWS)) {
			agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10";
		}

		uc.addRequestProperty("User-Agent", agent);

		final DataInputStream di = new DataInputStream(uc.getInputStream());
		final byte[] buffer = new byte[uc.getContentLength()];
		di.readFully(buffer);
		di.close();
		return new String(buffer);
	}
}
